//
//  syncdata.c
//  clublbiza
//
//  Created by WANG TOM on 7/18/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "syncdata.h"
#import "ApiLogin.h"
#import "SyncDataManager.h"
#import "SyncDownloadResource.h"

void syncdata_start()
{
    
    NSDictionary *msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"开始同步数据",@"msg",@"NOTICE",@"type", nil];
    sendNotificationMessage(msgData);
    
    ApiLogin *apiLogin;
    @try
    {
       apiLogin = [ApiLogin shareInstance];
      [apiLogin login];
    }@catch (NSException *exception) {
        
        NSDictionary *msgData = [NSDictionary dictionaryWithObjectsAndKeys:exception.reason,@"msg",@"ERROR",@"type", nil];
        sendNotificationMessage(msgData);
        return;
        
    }
    apiLogin.completedSync = NO;
    apiLogin.isBusySyncThread = NO;
    while (YES) {
      
        if ( apiLogin.completedSync ) {
            break;
        }
        if( apiLogin.isBusySyncThread ) {
            sleep(5);
            continue;
        }
        apiLogin.isBusySyncThread = YES;
        @try
        {
            msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"获取同步列表....",@"msg",@"NOTICE",@"type", nil];
            sendNotificationMessage(msgData);
        NSArray *sync_list = [[ApiLogin shareInstance] syncList];
            msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"获取同步列表完成",@"msg",@"NOTICE",@"type", nil];
            sendNotificationMessage(msgData);
            
        SyncDataManager *manager = nil;
        if ( [sync_list containsObject:@"beverage"] ) {
            msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"同步BEVERAGE....",@"msg",@"NOTICE",@"type", nil];
            sendNotificationMessage(msgData);
            
            manager = [[SyncDataManager alloc] initWithTableName:@"Beverage"];
            [manager syncData];
            [manager release];
            
            msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"同步BEVERAGE完成",@"msg",@"NOTICE",@"type", nil];
            sendNotificationMessage(msgData);
        }
        if ( [sync_list containsObject:@"beverage_item"] ) {
                msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"同步BEVERAGE列表....",@"msg",@"NOTICE",@"type", nil];
                sendNotificationMessage(msgData);
                
                manager = [[SyncDataManager alloc] initWithTableName:@"Beverage_item"];
                [manager syncData];
                [manager release];
                
                msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"同步BEVERAGE列表完成",@"msg",@"NOTICE",@"type", nil];
                sendNotificationMessage(msgData);
        }        

            if ( [sync_list containsObject:@"food"] ) {
                msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"同步FOOD....",@"msg",@"NOTICE",@"type", nil];
                sendNotificationMessage(msgData);
                
                manager = [[SyncDataManager alloc] initWithTableName:@"Food"];
                [manager syncData];
                [manager release];
                
                msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"同步Food完成",@"msg",@"NOTICE",@"type", nil];
                sendNotificationMessage(msgData);
            }
            if ( [sync_list containsObject:@"food_item"] ) {
                msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"同步FOOD列表....",@"msg",@"NOTICE",@"type", nil];
                sendNotificationMessage(msgData);
                
                manager = [[SyncDataManager alloc] initWithTableName:@"Food_item"];
                [manager syncData];
                [manager release];
                
                msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"同步FOOD列表完成",@"msg",@"NOTICE",@"type", nil];
                sendNotificationMessage(msgData);
            }  
            
        if ( [sync_list containsObject:@"ibizatv"] ) {
            msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"同步IBIZA TV列表....",@"msg",@"NOTICE",@"type", nil];
            sendNotificationMessage(msgData);
        manager = [[SyncDataManager alloc] initWithTableName:@"Ibizatv"];
        [manager syncData];
        [manager release];
            
            msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"同步IBIZA TV列表完成",@"msg",@"NOTICE",@"type", nil];
            sendNotificationMessage(msgData);
        }
            
        if ( [sync_list containsObject:@"slider_image"] ) {
                msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"同步图片列表....",@"msg",@"NOTICE",@"type", nil];
                sendNotificationMessage(msgData);
                manager = [[SyncDataManager alloc] initWithTableName:@"Slider_image"];
                [manager syncData];
                [manager release];
                
                msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"同步图片列表完成",@"msg",@"NOTICE",@"type", nil];
                sendNotificationMessage(msgData);
        }       
        
        if ( [sync_list containsObject:@"resource"] ) {
             msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"同步资源列表...",@"msg",@"NOTICE",@"type", nil];
            sendNotificationMessage(msgData);
        manager = [[SyncDataManager alloc] initWithTableName:@"Resource"];
        [manager syncData];
        [manager release];
             msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"同步资源列表完成....",@"msg",@"NOTICE",@"type", nil];
            sendNotificationMessage(msgData);
        }
        

            
         if ( [sync_list containsObject:@"delete"] ) {
              msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"删除过期数据....",@"msg",@"NOTICE",@"type", nil];
             sendNotificationMessage(msgData);
        manager = [[SyncDataManager alloc] initWithTableName:@"delete_record_log"];
        [manager syncData];
        [manager release];
              msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"删除过期数据完成",@"msg",@"NOTICE",@"type", nil];
             sendNotificationMessage(msgData);
         }
        //download resource
        msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"下载资源...",@"msg",@"NOTICE",@"type", nil];
            sendNotificationMessage(msgData);
        int downloadCount = [SyncDownloadResource dowanloadAll];
            
            apiLogin.totalDownloadVideoCount = downloadCount;
            if ( downloadCount == 0 ) {
        msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"下载资源完成....",@"msg",@"NOTICE",@"type", nil];
            
        
            msgData = [NSDictionary dictionaryWithObjectsAndKeys:@"同步完成",@"msg",@"DONE",@"type", nil];
            sendNotificationMessage(msgData);
                
                sleep(5);
                 apiLogin.isBusySyncThread = NO;
                apiLogin.completedSync = YES;
            }
        }@catch (NSException *exception) {

             NSDictionary *msgData = [NSDictionary dictionaryWithObjectsAndKeys:exception.reason,@"msg",@"ERROR",@"type", nil];
            sendNotificationMessage(msgData);
           
        }
    }
   
    
}

void sendNotificationMessage(NSDictionary* msgData)
{
    
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter postNotificationName:@"BACKGROUND_SYNC_THREAD_STATUS"
                                      object:nil
                                    userInfo:msgData];
}
